import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

// 布局组件
import Layout from '@/layout/Layout.vue'

// 页面组件
import Login from '@/views/auth/Login.vue'
import Register from '@/views/auth/Register.vue'
import Dashboard from '@/views/Dashboard.vue'
import BookList from '@/views/books/BookList.vue'
import BookDetail from '@/views/books/BookDetail.vue'
import BookManage from '@/views/books/BookManage.vue'
import UserList from '@/views/users/UserList.vue'
import UserProfile from '@/views/users/UserProfile.vue'
import BorrowList from '@/views/borrows/BorrowList.vue'
import BorrowManage from '@/views/borrows/BorrowManage.vue'
import CategoryManage from '@/views/categories/CategoryManage.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '仪表盘' }
      },
      {
        path: 'books',
        name: 'BookList',
        component: BookList,
        meta: { title: '图书列表' }
      },
      {
        path: 'books/:id',
        name: 'BookDetail',
        component: BookDetail,
        meta: { title: '图书详情' }
      },
      {
        path: 'admin/books',
        name: 'BookManage',
        component: BookManage,
        meta: { title: '图书管理', requiresAdmin: true }
      },
      {
        path: 'admin/users',
        name: 'UserList',
        component: UserList,
        meta: { title: '用户管理', requiresAdmin: true }
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: UserProfile,
        meta: { title: '个人信息' }
      },
      {
        path: 'borrows',
        name: 'BorrowList',
        component: BorrowList,
        meta: { title: '我的借阅' }
      },
      {
        path: 'admin/borrows',
        name: 'BorrowManage',
        component: BorrowManage,
        meta: { title: '借阅管理', requiresAdmin: true }
      },
      {
        path: 'admin/categories',
        name: 'CategoryManage',
        component: CategoryManage,
        meta: { title: '分类管理', requiresAdmin: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  console.log('路由守卫 - 目标路径:', to.path)
  console.log('路由守卫 - 认证状态:', authStore.isAuthenticated)
  console.log('路由守卫 - 是否需要认证:', to.meta.requiresAuth)
  
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    console.log('路由守卫 - 重定向到登录页')
    next('/login')
  } else if (to.meta.requiresAdmin && !authStore.isAdmin) {
    console.log('路由守卫 - 重定向到仪表盘')
    next('/dashboard')
  } else {
    console.log('路由守卫 - 允许访问')
    next()
  }
})

export default router 